package com.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.demo.common.base.ResultModel;
import com.demo.common.exception.BusinessException;
import com.demo.dao.UserMapper;
import com.demo.model.User;
import com.demo.model.UserInfo;
import com.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: UserServiceImpl
 * @Author: ChenXueLi
 * @Date: 2018-10-02 11:02
 */
@Service
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param user
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel addUser(User user, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return resultModel;
    }

    /**
     * 删除用户
     * @param user
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel deleteUser(User user, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserInfo loginUser = resultModel.getUserInfo();
        user = userMapper.getUserById(user.getUserId());
        if(user == null) {
            throw new BusinessException(-101, "用户信息查询失败");
        }
//        user.modify(loginUser);

        int effectRows = userMapper.deleteUser(user);

        resultModel.info("删除成功");

        return resultModel;
    }

    /**
     * 更新用户
     * @param user
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel updateUser(User user, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return resultModel;
    }

    /**
     * 根据id查询用户信息
     * @param user
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel getUserById(User user, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return resultModel;
    }

    /**
     * 获取用户列表
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel getUserList(ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PageHelper.startPage(resultModel.getPage(),  resultModel.getRows());

        List<User> userList = userMapper.getUserList(null);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        resultModel.setData(pageInfo);

        resultModel.info("查询成功");

        return resultModel;
    }

    /**
     * 快速修改用户状态
     *
     * @param user
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel modifyState(User user, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {

        int state = user.getUserState();

        user = userMapper.getUserById(user.getUserId());
        if(user == null) {
            throw new BusinessException(-101, "用户信息查询失败");
        }

        user.setUserState(state);

        int effectRows = userMapper.updateUser(user);

        resultModel.info();
        return resultModel;
    }
}
